Partial Public Class pages_Products_default : Inherits CPage

#Region "Querystring (Filters)"
    Public ReadOnly Property TypeId As Byte
        Get
            Return CWeb.RequestByte("typeId", EProductType.Sermon)
        End Get
    End Property
    Public ReadOnly Property SpeakerId As Integer
        Get
            Return CWeb.RequestInt("speakerId")
        End Get
    End Property
    Public ReadOnly Property ScribeId As Integer
        Get
            Return CWeb.RequestInt("scribeId")
        End Get
    End Property
    Public ReadOnly Property LanguageId As Integer
        Get
            Return CWeb.RequestInt("languageId")
        End Get
    End Property
    Public ReadOnly Property LocationId As Integer
        Get
            Return CWeb.RequestInt("locationId")
        End Get
    End Property
    Public ReadOnly Property ButtonId As Integer
        Get
            Return CWeb.RequestInt("buttonId")
        End Get
    End Property
    Public ReadOnly Property IsActive As Boolean?
        Get
            Return CWeb.RequestBoolNullable("isActive")
        End Get
    End Property
    Public ReadOnly Property Search As String
        Get
            Return CWeb.RequestStr("search")
        End Get
    End Property

    Public ReadOnly Property ShowDetails As Boolean
        Get
            Return CWeb.RequestBool("showDetails")
        End Get
    End Property
    Public ReadOnly Property ShowPictures As Boolean
        Get
            Return CWeb.RequestBool("showPictures")
        End Get
    End Property
    Public ReadOnly Property ShowPricing As Boolean
        Get
            Return CWeb.RequestBool("showPricing")
        End Get
    End Property
    Public ReadOnly Property ShowContents As Boolean
        Get
            Return CWeb.RequestBool("showContents")
        End Get
    End Property
    Public ReadOnly Property ShowTotals As Boolean
        Get
            Return CWeb.RequestBool("showTotals")
        End Get
    End Property

    Public ReadOnly Property TypeIdNullable As Byte?
        Get
            If TypeId = 0 Then Return Nothing Else Return TypeId
        End Get
    End Property
    Public ReadOnly Property SpeakerIdNullable As Byte?
        Get
            If Integer.MinValue = SpeakerId Then Return Nothing Else Return SpeakerId
        End Get
    End Property
    Public ReadOnly Property ScribeIdNullable As Byte?
        Get
            If Integer.MinValue = ScribeId Then Return Nothing Else Return ScribeId
        End Get
    End Property
    Public ReadOnly Property LanguageIdNullable As Byte?
        Get
            If Integer.MinValue = LanguageId Then Return Nothing Else Return LanguageId
        End Get
    End Property
    Public ReadOnly Property LocationIdNullable As Byte?
        Get
            If Integer.MinValue = LocationId Then Return Nothing Else Return LocationId
        End Get
    End Property
    Public ReadOnly Property IsSortable As Boolean
        Get
            Return TypeId = EProductType.ConferenceSet
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Products]() As CProductList
        Get
            Products = CProduct.Cache.Search(TypeIdNullable, SpeakerIdNullable, ScribeIdNullable, LanguageIdNullable, LocationIdNullable, ButtonId, Search, IsActive)
            If String.IsNullOrEmpty(Search) AndAlso TypeId = EProductType.Sermon Then Products = Products.SortByCreatedDescName
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        'SideMenu
        For Each i As CProductType In CProductType.Cache
            AddMenuSide(i.PluralAndActive, CSitemap.Products(Search, i.TypeId, SpeakerId, ScribeId, LanguageId, LocationId, ButtonId, IsActive, ShowDetails, ShowPictures, ShowPricing, ShowContents, ShowTotals), i.TypeId = TypeId)
        Next
        AddMenuSide("* All Products", CSitemap.Products(Search, 0, SpeakerId, ScribeId, LanguageId, LocationId, ButtonId, IsActive, False, False, False, True, False), TypeId = 0)
        AddMenuSide("* Top By Orders", CSitemap.Products(Search, 100, SpeakerId, ScribeId, LanguageId, LocationId, ButtonId, IsActive, False, False, False, False, True, New CPagingInfo(ctrlProducts.PageSize, 0, "OrdersCountCached", True)), TypeId = 100)
        AddMenuSide("* Top By Sales", CSitemap.Products(Search, 200, SpeakerId, ScribeId, LanguageId, LocationId, ButtonId, IsActive, False, False, False, False, True, New CPagingInfo(ctrlProducts.PageSize, 0, "PaymentsSumOkCached", True)), TypeId = 200)

        'Dropdowns
        For Each i As CSpeaker In CSpeaker.Cache
            Dim count As IList = CProduct.Cache.Search(TypeIdNullable, i.SpeakerId, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
            Dim name As String = CUtilities.NameAndCount(i.SpeakerName, count)
            CDropdown.Add(ddSpeaker, name, i.SpeakerId)
        Next
        CDropdown.BlankItem(ddSpeaker, "-- Any Speaker --")
        CDropdown.SetValue(ddSpeaker, SpeakerId)
        ddSpeaker.Font.Bold = SpeakerId > 0

        For Each i As CScribe In CScribe.Cache
            Dim count As IList = CProduct.Cache.Search(TypeIdNullable, Nothing, i.ScribeId, Nothing, Nothing, Nothing, Nothing, Nothing)
            Dim name As String = CUtilities.NameAndCount(CUtilities.Truncate(i.Name), count)
            CDropdown.Add(ddScribe, name, i.ScribeId)
        Next
        CDropdown.BlankItem(ddScribe, "-- Any Scribe --")
        CDropdown.SetValue(ddScribe, ScribeId)
        ddScribe.Font.Bold = ScribeId > 0


        For Each i As CButton In CButton.Cache
            CDropdown.Add(ddButton, CUtilities.NameAndCount(i.NameAndPrice, CProduct.Cache.Search(TypeIdNullable, Nothing, Nothing, Nothing, Nothing, i.ButtonId, Nothing, Nothing)), i.ButtonId)
        Next
        CDropdown.BlankItem(ddButton, "-- Any Price --")
        CDropdown.SetValue(ddButton, ButtonId)
        ddButton.Font.Bold = ButtonId > 0


        For Each i As CLanguage In CLanguage.Cache
            CDropdown.Add(ddLanguage, CUtilities.NameAndCount(i.LanguageName, CProduct.Cache.Search(TypeIdNullable, Nothing, Nothing, i.LanguageId, Nothing, Integer.MinValue, Nothing, Nothing)), i.LanguageId)
        Next
        CDropdown.BlankItem(ddLanguage, "-- Any Language --")
        CDropdown.SetValue(ddLanguage, LanguageId)
        ddLanguage.Font.Bold = LanguageId > 0


        For Each i As CLocation In CLocation.Cache
            CDropdown.Add(ddLocation, CUtilities.NameAndCount(CUtilities.Truncate(i.LocationName, 20), CProduct.Cache.Search(TypeIdNullable, Nothing, Nothing, Nothing, i.LocationId, Integer.MinValue, Nothing, Nothing)), i.LocationId)
        Next
        CDropdown.BlankItem(ddLocation, "-- Any Location --")
        CDropdown.SetValue(ddLocation, LocationId)
        ddLocation.Font.Bold = LocationId > 0



        CDropdown.Add(ddStatus, CUtilities.NameAndCount("Published", CProduct.Cache.Search(TypeIdNullable, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, True)), "True")
        CDropdown.Add(ddStatus, CUtilities.NameAndCount("Hidden", CProduct.Cache.Search(TypeIdNullable, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, False)), "False")
        CDropdown.BlankItem(ddStatus, "-- Any Status --")
        If IsActive.HasValue Then CDropdown.SetValue(ddStatus, IsActive.Value.ToString)
        ddStatus.Font.Bold = ddStatus.SelectedIndex > 0


        'Search state (from querystring)
        txtSearch.Text = Me.Search
        txtSearch.Font.Bold = Not String.IsNullOrEmpty(Search)

        chkShowDetails.Checked = ShowDetails
        chkShowImages.Checked = ShowPictures
        chkShowContents.Checked = ShowContents
        chkShowPricing.Checked = ShowPricing
        chkShowTotals.Checked = ShowTotals

        If TypeId > 0 AndAlso TypeId < 100 Then btnCreate.Text = "Create New " & CProductType.Cache.GetById(TypeId).TypeName & "..."

        'Display Results
        ctrlProducts.Display(Me.Products, ShowDetails, ShowPictures, ShowPricing, ShowContents, ShowTotals, Issortable)

        'Client-side
        Me.Form.DefaultFocus = txtSearch.ClientID  'txtCreate.ClientID
        Me.Form.DefaultButton = btnSearch.UniqueID  'CTextbox.OnReturnPress(txtSearch, btnSearch)

        CTextbox.OnReturnPress(txtSearch, btnSearch)
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click, ddStatus.SelectedIndexChanged, ddButton.SelectedIndexChanged, ddLanguage.SelectedIndexChanged, ddLocation.SelectedIndexChanged, ddScribe.SelectedIndexChanged, ddSpeaker.SelectedIndexChanged, chkShowImages.CheckedChanged, ddLanguage.SelectedIndexChanged, chkShowDetails.CheckedChanged, chkShowContents.CheckedChanged, chkShowPricing.CheckedChanged, chkShowTotals.CheckedChanged
        Dim isActive As Boolean? = Nothing
        If ddStatus.SelectedIndex > 0 Then isActive = IIf(ddStatus.SelectedIndex = 1, True, False)

        Response.Redirect(CSitemap.Products(txtSearch.Text, TypeId, CDropdown.GetInt(ddSpeaker), CDropdown.GetInt(ddScribe), CDropdown.GetInt(ddLanguage), CDropdown.GetInt(ddLocation), CDropdown.GetInt(ddButton), isActive, chkShowDetails.Checked, chkShowImages.Checked, chkShowPricing.Checked, chkShowContents.Checked, chkShowTotals.Checked))
    End Sub
    Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        Response.Redirect(CSitemap.ProductAdd(TypeId))
    End Sub
#End Region

#Region "Event Handlers - UserControl"
    Private Sub ctrl_AddClick() Handles ctrlProducts.AddClick
        btnCreate_Click(Nothing, Nothing)
    End Sub
    Private Sub ctrl_ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer) Handles ctrlProducts.ResortClick
        Response.Redirect(CSitemap.Products(txtSearch.Text, TypeId, SpeakerId, ScribeId, LanguageId, LocationId, ButtonId, IsActive, ShowDetails, ShowPictures, ShowPricing, ShowContents, ShowTotals, New CPagingInfo(0, pageNumber - 1, sortBy, descending)))
    End Sub
#End Region

End Class
